<?php
	/**
	* View Controller
	* This action will automatic determine the input param is view the categories, brand or view the product
	* 
	* The URL prototype
	* view/name/X|y|Z/                     :  Categories view
	* view/name/x                          :  Brand View
	* view_product/abc          :  Product view  
	* 
	* @author trungdt
	* @since Oct 3, 2011
	* 
	* Change logs
	* 
	*/

	if (!defined('EXPONENT')) exit('');
	$name = "";
	$name_type  = 0; // =0: seo_name; =1: id;
	if (isset($_REQUEST['name']))
		$name=_ab_safe_html_string($_REQUEST['name']);
	else 
	{
		header("Location: /");
		die();
	}
	if (is_int($name))
	{
		$name=intval($name);
		$name_type=1;
	}
	
	// determine now
	$name=explode("|",$name);
	if (count($name) > 0){
		$cat_browse=1; // == 0: brand browse
		if (count($name) == 1)
		{
			// check wherether brand_view or category_view
			if ($name_type == 0) // by seo_name
			{
				if (productsmodule::__GetBrandBySeoName($name[0])){
					// call to brand browse
					$brand_id=0;
					$brand_seo_name=$name[0];
					include("brand_view");
				}
				// search in brand not found: continue search in category
				else if (productsmodule::__GetCategoryBySeoName($name[0]))
				
				{
					// call to category browse
					$cat_id=0;
					$cat_seo_name=$name[0];
					include("categories_view.php");
				}
				// again: not found: show HTML_404
				else
					echo SITE_404_HTML;
			}
			else
			{
				// because, this is a int number, so, we should convert it to int first 
				// when $name[0] is int, it is category can not like catA|CatB
				$id=intval($name[0]); 
				if (productsmodule::__GetBrandByID($id)){
					// call to brand browse
					$brand_id=$id;
					$brand_seo_name="";
					include("brand_view.php");
				}
				// search in brand not found: continue search in category
				else if (productsmodule::__GetCategoryBySeoName($id))
				
				{
					// call to category browse
					$cat_id=$id;
					$cat_seo_name="";
					include("categories_view.php");
				}
				// again: not found: show HTML_404
				else
					echo SITE_404_HTML;
			}
		}
		else
		{
			// surely we know, cat browse
			$cat_id=""; // $name can not be int
			if (productsmodule::__GetCategoryBySeoName($name[count($name)-1])) // last element
			{
				$cat_seo_name=$name[count($name)-1];
				include("categories_view.php");
			}
			else
				echo SITE_404_HTML;
		}
	}

?>
